/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.controller;

import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.tools.DateUtils;
import com.apache.uct.common.BaseAction;
import com.apache.uct.common.entity.Act;
import com.apache.uct.common.entity.Lock;
import com.apache.uct.common.entity.Sys;
import com.apache.uct.common.entity.User;
import com.apache.uct.manager.ActManager;
import com.apache.uct.manager.LockManager;
import com.apache.uct.manager.SysManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * description:  系统访问控制管理Action
 *
 * @author iussoft 创建时间：2016-3-25
 */
@Controller
@RequestMapping("uct/lock/")
public class LockAction extends BaseAction<Lock> {

    @Autowired
    private LockManager lockManager;

    @Autowired
    private SysManager sysManager;

    @Autowired
    private ActManager actManager;

    /**
     * 功能说明：跳转到列表查询页面
     */
    @RequestMapping("list.action")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(LIST);
        view.addObject("sysId", request.getParameter("sysId"));
        view.addObject("sysEname", request.getParameter("sysEname"));
        return view;
    }

    @RequestMapping("lock!data.action")
    @ResponseBody
    protected Object data(HttpServletRequest request, String page, String rows) {
        ParamsVo<Lock> vo = new ParamsVo<Lock>();
        Lock lock = new Lock();
        if (Validator.isNotNull(request.getParameter("lockStatus"))) {
            lock.setLockStatus(request.getParameter("lockStatus"));
        }
        if (Validator.isNotNull(request.getParameter("lockCname"))) {
            lock.setLockCname(request.getParameter("lockCname"));
        }
        if (Validator.isNotNull(request.getParameter("lockPath"))) {
            lock.setLockPath(request.getParameter("lockPath"));
        }
        vo.setParams("pageIndex", page);
        vo.setParams("pageSize", rows);
        vo.setObj(lock);
        Page pageList = lockManager.getPageInfo(vo);
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("total", pageList.getCount());
        mapData.put("rows", pageList.getPageObjects());
        return mapData;
    }

    /**
     * 功能说明：跳转到新增页面
     */
    @Token(save = true)
    @RequestMapping(value = "add.action", method = RequestMethod.GET)
    protected ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(ADD);
        Lock lock = new Lock();
        if (Validator.isNotNull(request.getParameter("id"))) {
            ParamsVo<Lock> vo = new ParamsVo<Lock>();
            vo.setInfoId(request.getParameter("id"));
            lock = (Lock) lockManager.getInfoById(vo);
        } else {
            lock.setSysId(request.getParameter("sysId"));
            lock.setSysEname(request.getParameter("sysEname"));
        }
        view.addObject("item", lock);
        return view;
    }

    /**
     * 功能说明：通用保存页面(需要处理一下在黑名单)
     */
    @Token(remove = true)
    @RequestMapping(value = "/save.action", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg save(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute Lock item) {
        ResultMsg rmsg = new ResultMsg("T", "操作成功！");
        ParamsVo<Lock> vo = getParamsVo(request);
        long times = DateUtils.toLong(new Date());
        User user = this.getSessionUser(request);
        if (Validator.isNotNull(item.getLockId())) {//对信息修改
            item.setUpdateTime(times);
            item.setUpdateUser(user.getUserEname());
            vo.setObj(item);
            boolean mark = getBaseManager().editInfo(vo);
            if (!mark) {
                rmsg = getResultMsg(request, "信息修改失败！");
            }
        } else {//添加信息
            ParamsVo svo = new ParamsVo();
            svo.setInfoId(item.getSysId());
            Sys sys = (Sys) sysManager.getInfoById(svo);
            if (Validator.isNotNull(item.getActId())) {
                svo.setInfoId(item.getActId());
                Act act = (Act) actManager.getInfoById(svo);
                item.setActFullEname(act.getFullEname());
            }
            item.setSysCname(sys.getSysCname());
            item.setCreateUser(user.getUserEname());
            item.setCreateTime(times);
            vo.setObj(item);
            String id = getBaseManager().saveInfo(vo);
            if (Validator.isNull(id)) {
                rmsg = getResultMsg(request, "信息添加失败！");
            }
        }
        return rmsg;
    }

    /**
     * 功能说明：删除功能(需要处理一下在黑名单)
     *
     * @param response HttpServletResponse
     * @param id 删除的id
     */
    @RequestMapping(value = "/del.action")
    @ResponseBody
    public ResultMsg delete(HttpServletRequest request, String id) {
        ResultMsg rmsg = new ResultMsg("T", "删除成功！");
        ParamsVo<Lock> vo = getParamsVo(request);
        if (isNotNull(id)) {
            vo.setInfoId(id);
            boolean mark = getBaseManager().deleteInfo(vo);
            if (!mark) {
                rmsg.setFlag("F");
                rmsg.setMsg("删除失败！");
            }
        } else {
            rmsg.setFlag("H");
            rmsg.setMsg("请不要恶意操作！");
        }
        return rmsg;
    }

    protected BaseManager<Lock> getBaseManager() {
        return lockManager;
    }

    protected String getPrefix() {
        return "uct/lock-";
    }

}
